﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Login
{
    public partial class FormMgr : Form
    {
        public FormMgr()
        {
            InitializeComponent();
        }

        private void btnLoop_Click(object sender, EventArgs e)
        {
            if (this.Tag != null)
            {
                TreeNode tnRoot = new TreeNode(Name);
                tvControls.Nodes.Add(tnRoot);
                RecursiveControls(tnRoot, (Form)this.Tag);
                tvControls.ExpandAll();
            }            
        }

        //深度遍历
        void RecursiveControls(TreeNode tnParent, Control ctParent)
        {
            foreach (Control c in ctParent.Controls)
            {
                TreeNode tn = new TreeNode(c.Name);
                tn.Tag = c;
                tnParent.Nodes.Add(tn);
                RecursiveControls(tn, c);
            }
        }

        //获取树节点
        private void miDisabled_Click(object sender, EventArgs e)
        {
            Control ctl = GetControl();
            ctl.Enabled = false;
        }

        private void miEnabled_Click(object sender, EventArgs e)
        {
            Control ctl = GetControl();
            ctl.Enabled = true;
        }

        Control GetControl()
        {
            Control ctl = null;
            TreeNode tn = tvControls.SelectedNode;
            if (tn.Tag != null)
            {
                ctl = (Control)tn.Tag;
            }
            return ctl;
        }

        //树节点之外选择节点
        private void tvControls_MouseDown(object sender, MouseEventArgs e)
        {
            TreeNode clickedNode = tvControls.GetNodeAt(e.X, e.Y);
            if (clickedNode != null)
            {
                tvControls.SelectedNode = clickedNode;
            }
        }
    }
}
